﻿(function() {
    function BIT_S_G(id)
    { return document.getElementById(id); }
    function BIT_S_C(tg)
    { return document.createElement(tg); }
    var T = null;
    var S = BIT_S_G("sug"), I = BIT_S_G(sug_txt_name), SD, CS = null, K = false, mouseOnSug = false, E = false, c = -1, urls = "|";
    var isIE = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
    var isWebKit = (navigator.userAgent.indexOf("AppleWebKit/") > -1);
    var isGecko = (navigator.userAgent.indexOf("Gecko") > -1) && (navigator.userAgent.indexOf("KHTML") == -1);
    var timer3 = 0;

    function cs() {
        S.style.display = "none";
    }
    function ct() {
        var trs = T.childNodes;
        for (var i = 0; i < trs.length; i++) {
            trs[i].className = "ml";
        }
    }
    function clktr(a) {
        return function() {
            I.blur();
            u(a);
            sub(this.innerHTML);
        }
    }
    function sub(v) {
        sug_So();
    }
    function csc() {
        cs();
    }
    function setSug() {
        if (typeof (SD) != "object" || typeof (SD.s) == "undefined")
        { return; }
        var tab = BIT_S_C("ul");
        with (tab) {
            id = "sug_t";
        }
        for (var i = 0; i < SD.s.length; i++) {
            var tb = BIT_S_C("li");
            tab.appendChild(tb);
            var tr = tb;
            tr.onmouseover = function() { ct(); this.className = "mo"; mouseOnSug = true; };
            tr.onmouseout = ct;
            tr.onmousedown = function(e) { E = true; if (!isIE) { e.stopPropagation(); return false; } };
            tr.onclick = clktr(i);

            var keys = SD.s[i].split('|')[0];
            if (keys.indexOf(I.value) == 0)
                tr.innerHTML = I.value + '<b>' + keys.substring(I.value.length) + '</b>';
            else
                tr.innerHTML = keys;
        }

        var td = BIT_S_C("li");
        tab.appendChild(td);
        td.style.textAlign = "right";
        var oa = BIT_S_C("A");
        oa.href = "javascript:void(0)";
        oa.innerHTML = "关闭";
        oa.onclick = csc;

        td.appendChild(oa);

        S.innerHTML = "";
        S.appendChild(tab);
        S.style.width = (isIE ? I.offsetWidth + BIT_S_G("sug_submit").offsetWidth + 3 : I.offsetWidth + BIT_S_G("sug_submit").offsetWidth + 4) + "px";
        S.style.top = (isGecko ? I.clientHeight + 2 : I.clientHeight + 3) + "px";
        S.style.left = I.offsetLeft;

        S.style.display = "block";
        T = BIT_S_G("sug_t"); c = -1; s3 = "";
    }
    function kd(e) {
        e = e || window.event; K = false; var ctr;
        if (e.keyCode == 13) {
            sug_redirect();
        }
        if (e.keyCode == 38 || e.keyCode == 40) {
            mouseOnSug = false;
            if (S.style.display != "none") {
                var trs = T.childNodes; var l = trs.length - 1;
                for (var i = 0; i < l; i++) {
                    if (trs[i].className == "mo")
                    { c = i; break; }
                }
                ct(); var ctr;
                if (e.keyCode == 38) {
                    if (c == 0)
                    { I.value = SD.q; c = -1; K = true; }
                    else {
                        if (c == -1)
                        { c = l; }
                        ctr = trs[--c]; ctr.className = "mo"; u(c);
                    }
                }
                if (e.keyCode == 40) {
                    if (c == l - 1)
                    { I.value = SD.q; c = -1; K = true; }
                    else {
                        ctr = trs[++c];
                        ctr.className = "mo";
                        u(c);
                    }
                }
                if (!isIE)
                { e.preventDefault(); }
            }
        }
    }
    function u(c) {
        I.value = SD.s[c].split('|')[0];
    }
    window.bitauto = {
        sug: function(data) {
            if (typeof (data) == "object" && typeof (data.s) != "undefined" && typeof (data.s[0]) != "undefined")
            { SD = data; setSug(); }
            else {
                cs(); SD = {};
            }
        }, init: function() {
            s3 = I.value; timer1 = setInterval(f, 10);
        }
    };
    var s1 = ""; var s3; var timer2 = 0;
    function cb() {
        var b = true; var bs = I.value;
        if (typeof (T) != "undefined" && T != null) {
            var trs = T.childNodes;
            for (var i = 0; i < trs.length; i++) {
                if (trs[i].className == "mo") {
                    if (bs == trs[i].innerHTML.replace(/<[^>]*>/g, "") && !mouseOnSug)
                    { b = false; }
                }
            }
        }
        if (b && !K) {
            if (CS) {
                document.body.removeChild(CS);
            }
            CS = BIT_S_C("script");
            var sug_src = "http://59.151.102.96/sug0107.php?k=" + encodeURIComponent(I.value) + "&st=" + BIT_S_G("sug_datatype").value + "&ac=sug&d=" + (new Date()).getTime();
            if (typeof (BIT_S_G("sug_encoding").value) != 'undefined') {
                sug_src += "&en=" + BIT_S_G("sug_encoding").value;
            }
            CS.src = sug_src;
            document.body.appendChild(CS);
        }
    }
    function f() {
        var s2 = I.value;
        if (s2 == s1 && s2 != "" && s2 != s3) {
            if (timer2 == 0)
            { timer2 = setTimeout(cb, 100); }
        }
        else {
            clearTimeout(timer2);
            timer2 = 0;
            s1 = s2;
            if (s2 == "")
            { cs(); }
            if (s3 != I.value)
            { s3 = ""; }
        }
    }
    var timer1; I.onkeydown = kd; var isClkSug = false;
    I.onblur = function(e) {
        if (!isClkSug) {
            if (timer3 == 0)
            { timer3 = setTimeout(cs, 200); }
        }
        isClkSug = false;
        if (I.value.length == 0){
	    if(typeof(sug_txt_txt)=="string")
                I.value = sug_txt_txt;
	    else
	        I.className = "sug_onblur " + sug_txt_css;
	}
    };
    I.onfocus = function(e) {	
	if (I.value.length != 0)
            I.select();      
	if(typeof(sug_txt_txt)=="string")
	    if(I.value==(sug_txt_txt))
		I.value="";
        I.className = "sug_reset " + sug_txt_css;
    };
    document.onmousedown = function(e) {
        e = e || window.event; var elm = e.target || e.srcElement;
        if (elm == I)
        { return }
        while (elm = elm.parentNode) {
            if (elm == S || elm == I)
            { isClkSug = true; return; }
        }
        if (timer3 == 0)
        { timer3 = setTimeout(cs, 200); }
        cs();
    };
    window.onresize = function() {
        if (typeof (timer3) != "undefined" && timer3 != 0)
        { clearTimeout(timer3); }
        resetSuggestion();
    };
    S.style.zIndex = 200;
    function resetSuggestion() {
        if (S.style.display != "none") {
            setTimeout(function() {
                cs();
                if (SD != null)
                { setSug(SD); I.focus(); }
            }, 100)
        }
    }
    document.onkeydown = function(e) {
        if (isGecko) {
            e = e || window.event;
            if (e.ctrlKey) {
                if (e.keyCode == 61 || e.keyCode == 107 || e.keyCode == 109 || e.keyCode == 96 || e.keyCode == 48)
                { resetSuggestion(); }
            }
        }
    };

    I.onbeforedeactivate = function() {
        if (E)
        { window.event.cancelBubble = true; window.event.returnValue = false; E = false; }
    };
    I.setAttribute("autocomplete", "off");
})();

String.prototype.trimAll = function() {
    return this.replace(/(^\s*)|(\s*)|(\s*$)/g, "");
};

function sug_So() {
	BSearchRGo();
    document.sug_form.submit();
}

function sug_redirect() {}

function BSearchRGo() {
	if("undefined" == typeof(sug_flagID))
		sug_flagID = "-100";
    (new Image()).src = "http://www.cheyisou.com/rl.aspx?kw=" + document.getElementById("sug_txt_keyword").value + "&bc=-2&ct=" + sug_flagID + "&ts=" + (new Date()).getTime();
}